package com.example.logcat

import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.hdos.usbdevice.publicSecurityIDCardLib
import com.weijiaxing.logviewer.LogcatActivity
import java.util.*

class MainActivity : AppCompatActivity() {
    var bmpFile = ByteArray(38556)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 打开LogcatViewer监控
        LogcatActivity.launch(this);
        val imageView = findViewById<ImageView>(R.id.imageView)
        findViewById<Button>(R.id.button).setOnClickListener {
            val decodeInfo = arrayOfNulls<String>(13)
            val idCard = publicSecurityIDCardLib(this)
            Arrays.fill(bmpFile, 0x00.toByte())
            try {
                val ret =
                    idCard.PICC_Reader_ForeignerIDCard(decodeInfo, bmpFile, this.packageName)
                if (ret == 0) {
                    val colors: IntArray = idCard.convertByteToColor(bmpFile)
                    val bm = Bitmap.createBitmap(colors, 102, 126, Bitmap.Config.ARGB_8888)
                    bm.setHasAlpha(true)
                    val bm1 = Bitmap.createScaledBitmap(
                        bm,
                        (102 * 2), (126 * 2), false
                    )
                    imageView.setImageBitmap(bm1)
                    Log.d(TAG, "onCreate: ${decodeInfo.contentToString()}")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    companion object {
        private const val TAG = "MainActivity"
    }
}